package at.bartinger.gameoflive;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import at.bartinger.gameoflive.otherstuff.AboutDialog;
import at.bartinger.gameoflive.otherstuff.IntSetterDialog;
import at.bartinger.gameoflive.otherstuff.SeekBarDialog;
import at.bartinger.gameoflive.otherstuff.ZoomDialog;
import at.bartinger.gameoflive.otherstuff.IntSetterDialog.OkListener;
import at.bartinger.gameoflive.otherstuff.SeekBarDialog.OKListener;
import at.bartinger.gameoflive.otherstuff.ZoomDialog.ZoomListener;

public class GameOfLive extends Activity {

	private GameOfLiveView view;
	private DisplayMetrics dm;

	private IntSetterDialog cellsizeDialog;
	private AboutDialog aboutDialog;
	private SeekBarDialog seekDialog;
	private ZoomDialog zoomDialog;

	private int maxSpeed = 1500;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		int defaultCellsize = 40;

		setFullscreen(true);

		dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);

		view = new GameOfLiveView(this, dm, defaultCellsize);

		setContentView(view);

		aboutDialog = new AboutDialog(this);
		cellsizeDialog = new IntSetterDialog(this, "Cellsize (pixel)", defaultCellsize, new OkListener() {

			@Override
			public void onOkPressed(int setnumber) {
				view.setCellsize(setnumber);
			}
		},dm);
		seekDialog = new SeekBarDialog(this, "Speed", maxSpeed, 25, new OKListener() {
			
			@Override
			public void onOKpressed(int progress) {
				if(progress < 5){
					progress = 5;
				}
				view.setSpeed(maxSpeed-progress);
			}
		});
		zoomDialog = new ZoomDialog(this, "Zoom", new ZoomListener() {
			
			@Override
			public void onSeekBarChange(int progress) {
				view.doZoom(progress+100);
			}
			
			@Override
			public void onOKpressed(int progress) {
				zoomDialog.dismiss();
			}
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
			view.update();
		}
		return super.onKeyDown(keyCode, event);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.layout.menu, menu);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case R.id.playstop: view.toogleUpdates(); break;
		case R.id.cellsize: cellsizeDialog.show();  break;
		case R.id.zoom: zoomDialog.show();  break;
		case R.id.speed: seekDialog.show();  break;
		case R.id.eraser: view.toogleEraser();  break;
		case R.id.clear: view.clear();  break;
		case R.id.info: view.toogleInfo();  break;
		case R.id.aboutitem: aboutDialog.show();  break;
		}
		return super.onMenuItemSelected(featureId, item);
	}

	public void setFullscreen(boolean fullscreen){
		if(fullscreen){
			this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
			this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
			this.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		}else{
			this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
			this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
			this.getWindow().requestFeature(Window.FEATURE_CUSTOM_TITLE);
		}
	}
}